using System;
using System.Collections.Generic;

namespace MatrixFramework.Shared.Application.Files
{
    /// <summary>
    /// 文件信息DTO
    /// </summary>
    public class FileStorageDto
    {
        /// <summary>
        /// 文件ID
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 原始文件名
        /// </summary>
        public string OriginalFileName { get; set; }

        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// 文件MIME类型
        /// </summary>
        public string ContentType { get; set; }

        /// <summary>
        /// 文件大小（字节）
        /// </summary>
        public long FileSize { get; set; }

        /// <summary>
        /// 文件哈希值
        /// </summary>
        public string FileHash { get; set; }

        /// <summary>
        /// 存储路径
        /// </summary>
        public string StoragePath { get; set; }

        /// <summary>
        /// 访问URL
        /// </summary>
        public string AccessUrl { get; set; }

        /// <summary>
        /// 下载URL
        /// </summary>
        public string DownloadUrl { get; set; }

        /// <summary>
        /// 缩略图URL
        /// </summary>
        public string ThumbnailUrl { get; set; }

        /// <summary>
        /// 存储提供商
        /// </summary>
        public string StorageProvider { get; set; }

        /// <summary>
        /// 存储桶/容器名称
        /// </summary>
        public string BucketName { get; set; }

        /// <summary>
        /// 上传者用户ID
        /// </summary>
        public string CreatedById { get; set; }

        /// <summary>
        /// 上传者用户名
        /// </summary>
        public string CreatedByName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }

        /// <summary>
        /// 最后访问时间
        /// </summary>
        public DateTime? LastAccessTime { get; set; }

        /// <summary>
        /// 是否已删除
        /// </summary>
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 删除时间
        /// </summary>
        public DateTime? DeletionTime { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        public string TenantId { get; set; }

        /// <summary>
        /// 文件分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 文件标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 文件描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();

        /// <summary>
        /// 是否公开访问
        /// </summary>
        public bool IsPublic { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 下载次数限制
        /// </summary>
        public int? DownloadLimit { get; set; }

        /// <summary>
        /// 已下载次数
        /// </summary>
        public int DownloadCount { get; set; }

        /// <summary>
        /// 文件状态
        /// </summary>
        public FileStatus Status { get; set; }
    }

    /// <summary>
    /// 文件上传请求DTO
    /// </summary>
    public class FileUploadRequestDto
    {
        /// <summary>
        /// 原始文件名
        /// </summary>
        public string OriginalFileName { get; set; }

        /// <summary>
        /// 文件分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 文件描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 文件标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 是否公开访问
        /// </summary>
        public bool IsPublic { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 下载次数限制
        /// </summary>
        public int? DownloadLimit { get; set; }

        /// <summary>
        /// 自定义元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();

        /// <summary>
        /// 是否生成缩略图
        /// </summary>
        public bool GenerateThumbnail { get; set; }

        /// <summary>
        /// 存储策略
        /// </summary>
        public string StorageStrategy { get; set; }
    }

    /// <summary>
    /// 文件上传响应DTO
    /// </summary>
    public class FileUploadResponseDto
    {
        /// <summary>
        /// 上传是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 文件信息
        /// </summary>
        public FileStorageDto File { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; set; }

        /// <summary>
        /// 上传用时（毫秒）
        /// </summary>
        public long UploadDuration { get; set; }
    }

    /// <summary>
    /// 文件下载请求DTO
    /// </summary>
    public class FileDownloadRequestDto
    {
        /// <summary>
        /// 文件ID
        /// </summary>
        public string FileId { get; set; }

        /// <summary>
        /// 是否内联显示
        /// </summary>
        public bool Inline { get; set; }

        /// <summary>
        /// 缩略图尺寸
        /// </summary>
        public string ThumbnailSize { get; set; }

        /// <summary>
        /// 下载密码（如果有）
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 是否记录下载
        /// </summary>
        public bool RecordDownload { get; set; } = true;
    }

    /// <summary>
    /// 文件搜索参数DTO
    /// </summary>
    public class FileSearchInputDto
    {
        /// <summary>
        /// 关键词搜索
        /// </summary>
        public string Keyword { get; set; }

        /// <summary>
        /// 文件分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// MIME类型
        /// </summary>
        public string ContentType { get; set; }

        /// <summary>
        /// 文件大小范围
        /// </summary>
        public FileSizeRangeDto FileSizeRange { get; set; }

        /// <summary>
        /// 创建时间范围
        /// </summary>
        public DateTimeRangeDto CreationTimeRange { get; set; }

        /// <summary>
        /// 标签过滤
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 是否公开访问
        /// </summary>
        public bool? IsPublic { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        public string CreatedById { get; set; }

        /// <summary>
        /// 文件状态
        /// </summary>
        public FileStatus? Status { get; set; }

        /// <summary>
        /// 排序方式
        /// </summary>
        public string Sorting { get; set; }

        /// <summary>
        /// 跳过数量
        /// </summary>
        public int SkipCount { get; set; }

        /// <summary>
        /// 最大结果数量
        /// </summary>
        public int MaxResultCount { get; set; }
    }

    /// <summary>
    /// 文件大小范围DTO
    /// </summary>
    public class FileSizeRangeDto
    {
        /// <summary>
        /// 最小文件大小（字节）
        /// </summary>
        public long? MinSize { get; set; }

        /// <summary>
        /// 最大文件大小（字节）
        /// </summary>
        public long? MaxSize { get; set; }
    }

    /// <summary>
    /// 时间范围DTO
    /// </summary>
    public class DateTimeRangeDto
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 文件统计信息DTO
    /// </summary>
    public class FileStatisticsDto
    {
        /// <summary>
        /// 总文件数量
        /// </summary>
        public int TotalFiles { get; set; }

        /// <summary>
        /// 总文件大小
        /// </summary>
        public long TotalSize { get; set; }

        /// <summary>
        /// 按分类统计
        /// </summary>
        public Dictionary<string, int> FilesByCategory { get; set; } = new();

        /// <summary>
        /// 按扩展名统计
        /// </summary>
        public Dictionary<string, int> FilesByExtension { get; set; } = new();

        /// <summary>
        /// 按状态统计
        /// </summary>
        public Dictionary<string, int> FilesByStatus { get; set; } = new();

        /// <summary>
        /// 今日上传文件数
        /// </summary>
        public int FilesUploadedToday { get; set; }

        /// <summary>
        /// 本周上传文件数
        /// </summary>
        public int FilesUploadedThisWeek { get; set; }

        /// <summary>
        /// 本月上传文件数
        /// </summary>
        public int FilesUploadedThisMonth { get; set; }

        /// <summary>
        /// 平均文件大小
        /// </summary>
        public double AverageFileSize { get; set; }

        /// <summary>
        /// 最大文件大小
        /// </summary>
        public long MaxFileSize { get; set; }

        /// <summary>
        /// 最小文件大小
        /// </summary>
        public long MinFileSize { get; set; }
    }

    /// <summary>
    /// 文件状态枚举
    /// </summary>
    public enum FileStatus
    {
        /// <summary>
        /// 正常
        /// </summary>
        Normal = 0,

        /// <summary>
        /// 上传中
        /// </summary>
        Uploading = 1,

        /// <summary>
        /// 处理中
        /// </summary>
        Processing = 2,

        /// <summary>
        /// 已损坏
        /// </summary>
        Corrupted = 3,

        /// <summary>
        /// 已隔离
        /// </summary>
        Quarantined = 4,

        /// <summary>
        /// 已归档
        /// </summary>
        Archived = 5
    }

    /// <summary>
    /// 存储提供商枚举
    /// </summary>
    public enum StorageProvider
    {
        /// <summary>
        /// 本地存储
        /// </summary>
        Local = 0,

        /// <summary>
        /// MinIO
        /// </summary>
        MinIO = 1,

        /// <summary>
        /// 阿里云OSS
        /// </summary>
        AliyunOSS = 2,

        /// <summary>
        /// 腾讯云COS
        /// </summary>
        TencentCOS = 3,

        /// <summary>
        /// AWS S3
        /// </summary>
        AWSS3 = 4,

        /// <summary>
        /// Azure Blob Storage
        /// </summary>
        AzureBlob = 5
    }

    /// <summary>
    /// 文件分类常量
    /// </summary>
    public static class FileCategories
    {
        public const string Image = "image";
        public const string Video = "video";
        public const string Audio = "audio";
        public const string Document = "document";
        public const string Archive = "archive";
        public const string Other = "other";

        public static readonly string[] AllCategories = {
            Image, Video, Audio, Document, Archive, Other
        };
    }

    /// <summary>
    /// 文件扩展名常量
    /// </summary>
    public static class FileExtensions
    {
        public static readonly string[] ImageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg" };
        public static readonly string[] VideoExtensions = { ".mp4", ".avi", ".mov", ".wmv", ".flv", ".webm", ".mkv" };
        public static readonly string[] AudioExtensions = { ".mp3", ".wav", ".flac", ".aac", ".ogg", ".m4a" };
        public static readonly string[] DocumentExtensions = { ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt" };
        public static readonly string[] ArchiveExtensions = { ".zip", ".rar", ".7z", ".tar", ".gz" };

        public static string GetCategory(string extension)
        {
            var ext = extension.ToLowerInvariant();

            if (ImageExtensions.Contains(ext))
                return FileCategories.Image;

            if (VideoExtensions.Contains(ext))
                return FileCategories.Video;

            if (AudioExtensions.Contains(ext))
                return FileCategories.Audio;

            if (DocumentExtensions.Contains(ext))
                return FileCategories.Document;

            if (ArchiveExtensions.Contains(ext))
                return FileCategories.Archive;

            return FileCategories.Other;
        }
    }
}